using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Matrix.Configuration.Events;

namespace Matrix.Configuration.Services
{
    /// <summary>
    /// 配置变更通知服务接口
    /// 实现IOptionsMonitor模式的配置变更通知
    /// </summary>
    public interface IConfigurationChangeNotifier
    {
        /// <summary>
        /// 注册配置变更回调
        /// </summary>
        /// <param name="key">配置键</param>
        /// <param name="namespace">命名空间</param>
        /// <param name="callback">变更回调</param>
        /// <returns>订阅ID</returns>
        IDisposable SubscribeOnChange(string key, string? @namespace, Action<ConfigurationChangedEventArgs> callback);

        /// <summary>
        /// 注册类型化配置变更回调
        /// </summary>
        /// <typeparam name="T">配置类型</typeparam>
        /// <param name="key">配置键</param>
        /// <param name="namespace">命名空间</param>
        /// <param name="callback">变更回调</param>
        /// <returns>订阅ID</returns>
        IDisposable SubscribeOnChange<T>(string key, string? @namespace, Action<ConfigurationChangedEventArgs<T>> callback);

        /// <summary>
        /// 发布配置变更通知
        /// </summary>
        /// <param name="changeEvent">变更事件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发布任务</returns>
        Task NotifyChangeAsync(ConfigurationChangedEventArgs changeEvent, CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量发布配置变更通知
        /// </summary>
        /// <param name="changeEvents">变更事件列表</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发布任务</returns>
        Task NotifyChangesAsync(IEnumerable<ConfigurationChangedEventArgs> changeEvents, CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取指定配置的当前值
        /// </summary>
        /// <typeparam name="T">配置类型</typeparam>
        /// <param name="key">配置键</param>
        /// <param name="namespace">命名空间</param>
        /// <returns>配置值</returns>
        Task<T?> GetCurrentAsync<T>(string key, string? @namespace = null);

        /// <summary>
        /// 清除所有订阅
        /// </summary>
        void ClearAllSubscriptions();

        /// <summary>
        /// 获取订阅统计信息
        /// </summary>
        ConfigurationSubscriptionStatistics GetStatistics();
    }

    }